const Koa = require("koa")
const Router = require("koa-router")
const app = new Koa()
const router = new Router()
const path = require('path')
// const static = require('koa-static')
const static = require('koa-static-router');
const fs = require("fs")

router.get("/hello", (ctx, next) => {
  ctx.body = "hello tars"
})

app.use(static({
	dir:'./dist/static',
	router:'/static'
}))

router.get("/", async (ctx, next) => {
	ctx.response.type = 'html';
	ctx.response.body = fs.createReadStream('./dist/index.html');
})

app.use(router.routes())
const hostname = process.env.IP || "0.0.0.0"
const port = process.env.PORT || 3001

app.listen(port, hostname,()=>{
  console.log(`server listening at ${hostname}:${port}`)
})


// var http=require('http'); //用来启服务
// var fs=require('fs'); //用来读取文件
// var root="C:/Users/Administrator/Desktop/notice" //你本地放index.html页面的文件路径

//开启服务
// var server=http.createServer(function(req,res){
//     var url=req.url;
//     var file = root+url;
//     fs.readFile(file,function(err,data){
//         if(err){
//             res.writeHeader(404,{
//                 'content-type' : 'text/html;charset="utf-8"'
//             });
//             res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
//             res.end();
//         }else{
//             res.writeHeader(200,{
//                 'content-type' : 'text/html;charset="utf-8"'
//             });
//             res.write(data);//将index.html显示在客户端
//             res.end();

//         }
//     })
// }).listen(8888); //端口号
// console.log('服务器开启成功');

